﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Apache.Cassandra;

namespace Lucandra.Convenience
{
    internal static class CassandraHelper
    {
        /// <summary>
        /// Creates a Cassandra column from the given column name and value.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static Column CreateCassandraColumn(string name, byte[] value)
        {
            return new Column()
            {
                Name = Encoding.UTF8.GetBytes(name),
                Value = value,
                Timestamp = DateTime.UtcNow.Ticks
            };
        }

        public static Deletion CreateDeletion()
        {
            return new Deletion()
            {
                Timestamp = DateTime.UtcNow.Ticks
            };
        }

        public static Deletion CreateDeletion(SlicePredicate slicePredicate)
        {
            return new Deletion()
            {
                Predicate = slicePredicate,
                Timestamp = DateTime.UtcNow.Ticks
            };
        }

        public static Deletion CreateDeletion(byte[] superColumnName)
        {
            return new Deletion()
            {
                Super_column = superColumnName,
                Timestamp = DateTime.UtcNow.Ticks
            };
        }

        public static Deletion CreateDeletion(byte[] superColumnName, SlicePredicate slicePredicate)
        {
            return new Deletion()
            {
                Super_column = superColumnName,
                Predicate = slicePredicate,
                Timestamp = DateTime.UtcNow.Ticks
            };
        }

        public static KeyRange CreateKeyRange(byte[] startKey, byte[] endKey, int count)
        {
            return new KeyRange()
            {
                Start_key = startKey,
                End_key = endKey,
                Count = count
            };
        }

        public static KeyRange CreateKeyRange(string startToken, string endToken, int count)
        {
            return new KeyRange()
            {
                Start_token = startToken,
                End_token = endToken,
                Count = count
            };
        }

        public static ColumnParent CreateColumnParent(string columnFamily)
        {
            return new ColumnParent()
            {
                Column_family = columnFamily
            };
        }

        public static ColumnParent CreateColumnParent(byte[] superColumnName)
        {
            return new ColumnParent()
            {
                Super_column = superColumnName
            };
        }

        public static SliceRange CreateSliceRange(byte[] start, byte[] finish, bool reversed, int count)
        {
            return new SliceRange()
            {
                Start = start,
                Finish = finish,
                Reversed = reversed,
                Count = count
            };
        }

        public static SliceRange CreateSliceRange(bool reversed, int count)
        {
            return CassandraHelper.CreateSliceRange(new byte[0], new byte[0], reversed, count);
        }

        public static SlicePredicate CreateSlicePredicate(SliceRange sliceRange, IEnumerable<byte[]> columnNames)
        {
            return new SlicePredicate()
            {
                 Slice_range = sliceRange,
                 Column_names = columnNames.ToList()
            };
        }

        public static SlicePredicate CreateSlicePredicate(IEnumerable<string> columnNames)
        {
            var slicePredicate = new SlicePredicate();
            slicePredicate.Column_names = new List<byte[]>();
            foreach (var name in columnNames)
                slicePredicate.Column_names.Add(Encoding.UTF8.GetBytes(name));
            return slicePredicate;
        }

        public static SlicePredicate CreateSlicePredicate(SliceRange sliceRange)
        {
            return new SlicePredicate()
            {
                Slice_range = sliceRange
            };
        }
    }
}
